<template>
  <div class="login-container">
    <el-row style="width: 100%; padding: 10em 10px 5em 10px;">
      <el-col :md="{ span: 10 }" :lg="{ span: 6 }" :xs="{ span: 24 }" :sm="{ span: 14 }" class="el-col-login-form"
        style=''>
        <el-card class="login-form">
          <div slot="header" style="color: #2b3d51; font-weight: 600; font-size: 15px;">
            欢迎登陆
          </div>

          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" auto-complete="on" label-position="left">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="用户名">
                <span slot="prefix" class="svg-container">
                  <svg-icon icon-class="user" />
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">

              <el-input :type="pwdType" v-model="loginForm.password" name="password" auto-complete="on" placeholder="密码"
                @keyup.enter.native="handleLogin">
                <span slot="prefix" class="svg-container">
                  <svg-icon :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'" />
                </span>
              </el-input>
              <span class="show-pwd" @click="showPwd"></span>
            </el-form-item>
            <el-form-item>
              <div style="background: #fff">
                <el-row>
                  <el-col :span="16" style="padding-left: 5px; padding-top: 8px;">
                    <el-radio v-model="savePwd" label="记住密码">记住密码</el-radio>
                  </el-col>
                  <el-col :span="8" style="text-align: right">
                    <el-button class="btn" :loading="loading" type="primary"
                      style="width: 100%; padding: 10px 15px; border-radius: 25px; font-size: 14px;"
                      @click.native.prevent="handleLogin">
                      登录 Log In
                    </el-button>
                  </el-col>
                </el-row>
              </div>
            </el-form-item>
            <el-form-item>
              <div style="background: #fff">
                <el-row>
                  <el-col :span="16" style="padding-left: 5px;">
                    <div @click="handleHome">
                      <svg-icon icon-class="left-arrow"
                        style="font-weight: bold;font-size: 16px;margin-right: 3px;"></svg-icon>
                      <el-button label="返回主页" type="text"
                        style="font-weight: bold;font-size: 16px;color: #6c757d;">返回主页</el-button>
                    </div>
                  </el-col>
                  <el-col :span="8">
                    <div @click="handleRegistered">
                      <svg-icon icon-class="user1"
                        style="font-weight: bold;font-size: 16px;margin-right: 3px;"></svg-icon>
                      <el-button label="新会员注册" type="text"
                        style="font-weight: bold;font-size: 16px;color: #6c757d;">新会员注册</el-button>
                    </div>
                  </el-col>
                </el-row>
              </div>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import md5 from 'js-md5'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          this.$RegExp.required()
        ],
        password: [
          this.$RegExp.required()
        ]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined,
      savePwd: ''
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleHome() {
      location.replace(location.origin + '/usr/index.html#/index')
    },
    handleRegistered() {
      location.replace(location.origin + '/usr/index.html#/registor')
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', Object.assign({ password: md5(this.loginForm.password), username: this.loginForm.username }))
            .then(() => {
              this.loading = false
              this.$router.push({ path: this.redirect || '/' })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;

    input {
      height: 40px;
      background: transparent;
      border: unset;
      color: #000;
      font-size: 16px;
    }
  }

  .el-form-item {
    border-radius: 20px;
    background: #e8f0fe;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: url("../../assets/img/login-bg.jpg") no-repeat center;
  background-size: cover;

  .el-col-login-form {
    max-width: 420px;
    float: right;
    margin-right: 5%
  }

  .login-form {
    background: #fff;
  }

  .tips {
    font-size: 14px;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 0px;
    color: #c8c8c8;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

/deep/ .el-card {
  border-radius: 15px;
}

/deep/ .el-radio__label {
  color: #2b3d51;
  font-size: 16px;
}

@media screen and (max-width: 768px) {
  .login-container {
    padding: 5em 10px 3em 10px;
  }

  .el-col-login-form {
    max-width: 420px;
    float: right;
    margin-right: 0 !important;
  }

  .login-form {
    max-width: 100% !important;
    margin-right: 0 !important;
    float: none !important;
  }

  .el-form-item {
    margin-bottom: 20px;
  }

  .el-button {
    font-size: 14px;
  }

  .el-input {
    width: 100% !important;
  }

  .el-radio {
    line-height: 24px;
  }
}
</style>
